package com.woniuxy.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.*;
import com.woniuxy.service.*;
import org.apache.poi.poifs.property.Child;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜品Controller
 */
@Controller
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private DishCategoryService dishCategoryService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private UserService userService;
    @Autowired
    private VDishService vDishService;
    @Autowired
    private VReviewService vReviewService;
    @Autowired
    private UserShopService userShopService;
    @Autowired
    private SpecsService specsService;

    //首页跳转详情页
    @GetMapping("details/{dishId}")
    public String dishDetails(@PathVariable int dishId,HttpSession session){
        //session存储当前菜品id
        session.setAttribute("dishId",dishId);
        return "detailsp";
    }
    //订单详情页查数据
    @GetMapping("dishDetails")
    @ResponseBody
    public ResponseEntity dishDetails(Integer current,HttpSession session){
        if (current == null || current == 0){
            current = 1;
        }
        int dishId = (int) session.getAttribute("dishId");
        VDish dish = vDishService.getById(dishId);
        QueryWrapper<VReview> queryWrapper = new QueryWrapper<>();
        if(dishId != 0){
            queryWrapper.eq("dishId",dishId);
        }
        Page<VReview> page = new Page<>(current,1);
        ArrayList list = new ArrayList();
        //存储菜品
        list.add(dish);
        //存储该菜品的所有评论信息
        list.add(vReviewService.page(page,queryWrapper));
        //存储属性，属性值相关内容
        list.add(specsService.getSpecsByDishId(dishId));
        return new ResponseEntity(list);
    }
    @GetMapping("goShop/{id}")
    public String goShop(@PathVariable int id){
        return "redirect:/dish/list/"+dishService.getShopByDishId(id).getId();
    }
    //商铺详情页查数据
    //current为菜谱当前页码，current1为累计评论当前页码
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity list(Integer current,Integer current1, HttpSession session){
        int shopId = (int) session.getAttribute("shopId");
        User user = (User) session.getAttribute("user");
        if (current == null || current == 0){
            current = 1;
        }
        if (current1 == null || current1 == 0){
            current1 = 1;
        }
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        QueryWrapper<VReview> queryWrapper1 = new QueryWrapper<>();
        if(shopId != 0){
            queryWrapper.eq("shopId",shopId);
            queryWrapper1.eq("shopId",shopId);
        }
        Page<Dish> page = new Page<>(current,4);
        Page<VReview> page1 = new Page<>(current1,1);
        ArrayList list = new ArrayList();
        //存储商铺信息
        list.add(shopService.getById(shopId));
        //存储菜单分页数据
        list.add(dishService.page(page,queryWrapper));
        //存储该商铺所有的评论
        list.add(vReviewService.page(page1,queryWrapper1));
        //判断该用户是否收藏了该商铺
        if(user != null) {
            list.add( userService.collectIsExist(user.getId(), shopId));
        }
        return new ResponseEntity(list);
    }

    /**
     * 进入店铺详情页   查找店铺是否被用户收藏
     */

    @PostMapping("shop/{dishId}")
    @ResponseBody
    public ResponseEntity sel(@PathVariable int dishId, HttpSession session){
        User user =(User) session.getAttribute("user");
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(501);
        if(user != null) {
            int count = userShopService.selShop(user.getId(), dishId);
            if (count > 0) {
                return ResponseEntity.SUCCESS;
            } else {
                return responseEntity;
            }
        }
         responseEntity.setCode(502);
        return responseEntity;
    };

    /**
     * 添加收藏
     * @param session
     * @param id
     * @return
     */
    @PostMapping("collectShop/{id}")
    @ResponseBody
    public ResponseEntity collectShop(HttpSession session,@PathVariable int id){

        System.out.println(id+"++++++++++++++++++++++++");
        User user = (User) session.getAttribute("user");
        userService.collectShop(user.getId(),id);
        return ResponseEntity.SUCCESS;
    }

  /*  @RequestMapping("noCollectShop/{id}")*/

    /**
     * 取消收藏
     * @param session
     * @param id
     * @return
     */
    @PostMapping("noCollectShop/{id}")
    @ResponseBody
    public ResponseEntity noCollectShop(HttpSession session, @PathVariable int id){
        System.out.println(id+"++++++++++++++++++++++++");
        User user = (User) session.getAttribute("user");
        userService.noCollectShop(user.getId(), id);
        return ResponseEntity.SUCCESS;
    }
    //header进所有菜品
    @GetMapping("allDishes")
    public String goAllDishes(){
        return "category";
    }
    //首页—>所有菜品数据查询
    @GetMapping("dishList")
    @ResponseBody
    public ResponseEntity getDishList(Integer current, Integer cid,Integer rid,Integer minPrice,Integer maxPrice){
        if (current == null || current == 0){
            current = 1;
        }
        QueryWrapper<VDish> queryWrapper = new QueryWrapper<>();
        //行政区查询条件
        if(rid != null && rid != 0){
           /* if(shopService.getByRegionId(rid) != null && shopService.getByRegionId(rid).size() != 0 && shopService.getByRegionId(rid).size() != 1) {

                for(Shop shop : shopService.getByRegionId(rid)){
                        queryWrapper.or(wrapper -> wrapper.eq("shopId", shop.getId()));
                }
            }else if(shopService.getByRegionId(rid).size() == 0){
                queryWrapper.eq("shopId",0);
            }else if(shopService.getByRegionId(rid).size() == 1){
                queryWrapper.eq("shopId",shopService.getByRegionId(rid));
            }*/
            queryWrapper.like("address",regionService.getById(rid).getRegion());
        }
        //菜品类别查询条件
        if (cid != null &&cid != 0){
            queryWrapper.eq("dishCategoryId",cid);
        }
        //菜品价格查询条件
        //价格在两个价格之间
        if(minPrice != 0 & maxPrice != 0){
            queryWrapper.between("price",minPrice,maxPrice);
        }
        //价格小于某个价格
        if(minPrice != 0 && maxPrice == 0){
            queryWrapper.lt("price",minPrice);
        }
        //价格大于某个价格
        if(minPrice == 0 && maxPrice != 0){
            queryWrapper.gt("price",maxPrice);
        }
        Page<VDish> page = new Page<>(current,6);
        ArrayList<Object> list = new ArrayList<>();
        //存储菜品类别
        list.add(dishCategoryService.list());
        //存储菜品所属地区分类
        list.add(regionService.getArea());
        //存储菜品分页数据
        list.add(vDishService.page(page,queryWrapper));
        //
        return new ResponseEntity(list);
    }

    /**
     * 搜索框，根据关键字实现模糊查询菜品
     * @return
     */
    @GetMapping("search")
    public String search(String keyword,Integer current,Model model){
        if (current == null || current == 0){
            current = 1;
        }
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        if (!"".equals(keyword)){
            queryWrapper.like("name", "%"+keyword+"%");
        }
        Page<Dish> page = new Page<>(current,3);
        model.addAttribute("keyword", keyword);
        model.addAttribute("page",dishService.page(page,queryWrapper));



        return "search_s";
    }

}
